package com.thwj.project.app.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.thwj.project.app.api.vo.MerchantProductVO;
import com.thwj.project.biz.domain.MerchantProduct;
import com.thwj.project.biz.enums.MerchantProductStatusEnum;
import com.thwj.project.biz.service.IMerchantProductService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Transactional
@Service
public class AppProductService {

    @Autowired
    IMerchantProductService merchantProductService;

    public MerchantProductVO getProductById(Long productId) {
        MerchantProduct product = merchantProductService.getById(productId);
        return convert(product);
    }


    public List<MerchantProductVO> getMerchantProductVOList(Long merchantId) {

        List<MerchantProduct> productList = merchantProductService.list(
                new QueryWrapper<MerchantProduct>().eq("merchant_id", merchantId)
                        .eq("status", MerchantProductStatusEnum.up.getValue())
                        .eq("valid", true)
        );
        List<MerchantProductVO> productVOList = new ArrayList<>();
        productList.forEach(item->{
            productVOList.add(convert(item));
        });

        return productVOList;
    }



    public MerchantProductVO convert(MerchantProduct product) {
        MerchantProductVO productVO = new MerchantProductVO();
        productVO.setId(product.getId());
        productVO.setMerchantId(product.getMerchantId());
        productVO.setProductName(product.getProductName());
        productVO.setProductPic(product.getProductPic());
        productVO.setSuggestedNumber(product.getSuggestedNumber());
        productVO.setPrice(product.getPrice());
        productVO.setSaleCount(product.getSaleCount());
        productVO.setDescPicList(product.getDescPicList());
        productVO.setOriginalPrice(product.getOriginalPrice());
        return productVO;
    }
}
